// utils.c
#include "../utils/utils.h"
#include <stdint.h>
#include <stdio.h>
#include <math.h>

FILE* safe_fopen(const char* filename, const char* mode) {
    FILE* file = fopen(filename, mode);
    if (file == NULL) {
        fprintf(stderr, "Error: Failed to open file %s\n", filename);
        exit(EXIT_FAILURE);
    }
    return file;
}

uint32_t read_big_endian_int(FILE* file) {
    uint8_t bytes[4];
    if (fread(bytes, sizeof(uint8_t), 4, file) != 4) {
        fprintf(stderr, "Error: Failed to read 32-bit integer from file\n");
        exit(EXIT_FAILURE);
    }
    return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}

// 激活函数 (ReLU)
void active_function(int size, double input[1][size], double output[1][size]) {
    for (int i = 0; i < size; i++) {
        output[0][i] = fmax(0, input[0][i]); // ReLU 函数
    }
}